package com.bang;

import cn.hutool.core.io.FileUtil;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.main.MainResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.File;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: EsUtils
 * @Desc: TODO
 * @author: <Bang>
 * @Date: 2021/12/22 16:34
 * @Version: 1.0
 * @Modified By:
 */
public class EsUtils {

    private static final String esUrl = "192.168.96.173";
    private static final String userName = "admin";
    private static final String password = "vrv@123456";

    private static volatile RestHighLevelClient client;

    public static RestHighLevelClient getClient() {
        if (null == client) {
            synchronized (RestHighLevelClient.class) {
                if (null == client) {
                    RestClientBuilder builder = RestClient.builder(
                            new HttpHost(esUrl, 9200, "http"));
                    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
                    builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
                    client = new RestHighLevelClient(builder);
                }
            }
        }
        return client;

    }

    public static RestClient getLowLevelClient() {
        return getClient().getLowLevelClient();

    }

    public static void main(String[] args) throws Exception {
        Map<String,String> map=new HashMap<>();
        File file = new File("D:\\study\\java_foundation\\parent\\elastic-parent\\elastic6\\src\\main\\resources\\esMappings");
        if (file.isDirectory()){
            File[] files = file.listFiles();
            for (File f : files) {
                String s = FileUtil.readString(f, Charset.defaultCharset());
                map.put(f.getName(),s);
            }
        }

        RestClientBuilder builder = RestClient.builder(
                new HttpHost("192.168.96.156", 9200, "http"));
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        for (String s : map.keySet()) {
            PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest(s);
            putIndexTemplateRequest.source(map.get(s), XContentType.JSON);
            AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().putTemplate(putIndexTemplateRequest, RequestOptions.DEFAULT);
        }


    }
}
